﻿using System.Windows;
using System.Windows.Controls;

using NavFx;

namespace NavFx.NavFxDemo
{
	/// <summary>
	/// Page that demonstrates how to use NavFx to implement a Wizard
	/// </summary>
	public partial class Wizard: HostPage
	{
		#region Overrides

		/// <summary>
		/// Wizard needs it's own Navigator to avoid clashing with pages in the cache of the application Navigator
		/// </summary>
		public override Navigator Navigator
		{
			get;
			set;
		}

		#endregion

		#region Private members

		/// <summary>
		/// Tracks index of current page
		/// </summary>
		private int currentIndex = 0;

		/// <summary>
		/// Reference to the component handling transitions from page to page
		/// </summary>
		private SimpleTransitor navTransitor = null;


		/// <summary>
		/// Helper method ti set state of the buttons based on the current page
		/// </summary>
		private void setButtonState()
		{
			this.previous.IsEnabled = (this.currentIndex != 0);
			this.next.IsEnabled = (this.currentIndex != 2);
			this.finish.IsEnabled = (this.currentIndex == 2);
		}


		/// <summary>
		/// Helper method to navigate the application home page
		/// </summary>
		private void goHome()
		{
			Application.Current.GetNavigator().GetTransitor<SimpleTransitor>().Target = Application.Current;
			Application.Current.GetNavigator().GoToPage("Home");
		}

		#endregion

		#region Constructors

		/// <summary>
		/// Default constructor
		/// </summary>
		public Wizard()
		{
			//capture a reference to the component that handles
			//transition from page to page
			this.Navigator = new Navigator();
			this.Navigator.Transitor = new SimpleTransitor(this.pageContainer);
			this.navTransitor = this.Navigator.GetTransitor<SimpleTransitor>();

			InitializeComponent();
		}

		#endregion

		#region Event handlers

		/// <summary>
		/// Navigate to previous page
		/// </summary>
		private void previous_Click(object sender, RoutedEventArgs e)
		{
			//display the appropriate page
			switch(this.currentIndex)
			{
				case 1:
					this.Navigator.GoToPage("WizardPage1");
					break;
				case 2:
					this.Navigator.GoToPage("WizardPage2");
					break;
				default:
					break;
			}

			//make sure the index is valid
			this.currentIndex--;
			if(this.currentIndex < 0)
			{
				this.currentIndex = 0;
			}

			//update state of buttons
			this.setButtonState();
		}

		/// <summary>
		/// Navigate to next page
		/// </summary>
		private void next_Click(object sender, RoutedEventArgs e)
		{
			//display the appropriate page
			switch(this.currentIndex)
			{
				case 0:
					this.Navigator.GoToPage("WizardPage2");
					break;
				case 1:
					this.Navigator.GoToPage("WizardPage3");
					break;
				default:
					break;
			}

			//make sure the index is valid
			this.currentIndex++;
			if(this.currentIndex > 2)
			{
				this.currentIndex = 2;
			}

			//update state of buttons
			this.setButtonState();
		}

		/// <summary>
		/// Return the application home page on click of the finish button
		/// </summary>
		private void finish_Click(object sender, RoutedEventArgs e)
		{
			this.goHome();
		}

		/// <summary>
		/// Return to the application home page on click of the cancel button
		/// </summary>
		private void cancel_Click(object sender, RoutedEventArgs e)
		{
			this.goHome();
		}

		/// <summary>
		/// Load the first page of the wizard on loading of this page
		/// </summary>
		private void Wizard_Loaded(object sender, RoutedEventArgs e)
		{
			//Transitor is set on creation safe
			//to assume it exists as using local
			//version and not application Navigator
			this.Navigator.GoToPage("WizardPage1");
		}

		#endregion
	}
}
